package virtorina.client.admin;

import java.util.Collections;
import java.util.LinkedList;

import virtorina.client.admin.rpc.AdminService;
import virtorina.client.room.dto.ComplInfo;
import virtorina.client.room.dto.ComplaintDTO;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Complaints implements EntryPoint {

	@Override
	public void onModuleLoad() {
		AdminService.Util.getInstance().getAccessToComplains(
				new AsyncCallback<Boolean>() {

					@Override
					public void onSuccess(Boolean result) {
						result = true;
						if (result) {
							show();
						} else {
							RootPanel
									.get("list")
									.add(new HTML(
											"<h1 style=\"size: 400px;\"><a href=\"/\">This page is only available to the adminisation. Boost your IQ & join the moderator team!</a></h1>"));
						}

					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});/*show();*/
	}

	void show() {

		final VerticalPanel panel = new VerticalPanel();
		RootPanel.get("list").add(panel);
		AdminService.Util.getInstance().getTopComplaints(
				new AsyncCallback<LinkedList<ComplaintDTO>>() {

					@Override
					public void onSuccess(LinkedList<ComplaintDTO> result) {
						StringBuilder s;
						for (ComplaintDTO c : result) {
							s = new StringBuilder("<b>Question</b>: "
									+ c.getQuestion() + "<br><b>Answers</b>: ");
							for (String a : c.getAnswers()) {
								s.append("{" + a + "} ");
							}
							s.append("<br><b>Subjects</b>: ");
							for (String a : c.getLabels()) {
								s.append("{" + a + "} ");
							}
							for (ComplInfo ci : c.getComplaints()) {
								s.append("<br>Complaint: <b>");
								switch (ci.getType()) {
								case 0:
									s.append("boring");
									break;
								case 1:
									s.append("ambiguous");
									break;
								case 2:
									s.append("violative");
									break;
								case 3:
									s.append("incorrect");
									break;
								case 4:
									s.append("lacking subjects");
									break;
								}
								s.append("</b> (" + ci.getNumber()
										+ " persons)");
							}
							panel.add(new HTML(s + "<br><hr>".toString()));
						}

					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});

	}

}
